<template>
<div class="sidebar">
  <div class="logo-container">
    <img src="https://b.zmxai.com/pc/assets/logo_layout-DkcrJCB-.png" alt="Logo" class="logo">
  </div>
  <el-menu
    :default-active="activeIndex"
    class="el-menu-vertical"
    background-color="#F5F7FA"
    text-color="#303133"
    active-text-color="#409EFF"
    :unique-opened="true"
    @select="handleSelect"
  >
    <el-menu-item index="home">
      <el-icon><House /></el-icon>
      <span>主页</span>
    </el-menu-item>
    
    <div class="menu-section-title">
      <span>AI创作</span>
    </div>
    <el-menu-item index="ai-chat">
        <el-icon><List /></el-icon>
      <span class="sub-menu-indent">文本创作</span>
    </el-menu-item>
    <el-menu-item index="ai-video">
        <el-icon><VideoCameraFilled /></el-icon>
      <span class="sub-menu-indent">视频创作</span>
    </el-menu-item>
    <el-menu-item index="ai-image">
        <el-icon><PictureFilled /></el-icon>
      <span class="sub-menu-indent">图像处理</span>
    </el-menu-item>
    <el-menu-item index="ai-voice"> 
        <el-icon><Promotion /></el-icon>
      <span class="sub-menu-indent">语音合成</span>
    </el-menu-item>
  </el-menu>
    <div style="flex: 1;"></div>
    
    <div class="login-button-section">
      <el-button 
        type="primary" 
        class="login-btn"
        size="large"
        @click="handleLogin"
      >
      <el-icon class="login-icon"><UserFilled /></el-icon>
        登录
      </el-button>
    </div>
    
    <div class="user-login-section">
      <div class="user-info">
        <el-avatar :size="25" :icon="UserFilled"/>
        <div class="user-details">
          <span class="user-name">未登录</span>
        </div>
      </div>
    </div>
</div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { House, Aim, VideoCameraFilled, PictureFilled, Promotion, UserFilled ,List} from '@element-plus/icons-vue'

const activeIndex = ref('home')
const router = useRouter()
const route = useRoute()

// 根据当前路由路径设置activeIndex
const setActiveIndexByRoute = () => {
  const currentPath = route.path
  
  // 根据路径映射到对应的菜单项索引
  if (currentPath === '/') {
    activeIndex.value = 'home'
  } else if (currentPath === '/ai/chat') {
    activeIndex.value = 'ai-chat'
  } else if (currentPath === '/ai/video') {
    activeIndex.value = 'ai-video'
  } else if (currentPath === '/ai/image') {
    activeIndex.value = 'ai-image'
  } else if (currentPath === '/ai/voice') {
    activeIndex.value = 'ai-voice'
  }
}

const handleSelect = (index: string) => {
  activeIndex.value = index
  
  // 根据不同的菜单项索引跳转到不同的页面
  switch(index) {
    case 'home':
      router.push('/')
      break
    case 'ai-chat':
      router.push('/ai/chat')
      break
    case 'ai-video':
      router.push('/ai/video')
      break
    case 'ai-image':
      router.push('/ai/image')
      break
    case 'ai-voice':
      router.push('/ai/voice')
      break
    default:
      break
  }
}

// 组件挂载时设置当前激活的菜单项
onMounted(() => {
  setActiveIndexByRoute()
  
  // 监听路由变化，实时更新激活的菜单项
  router.afterEach(() => {
    setActiveIndexByRoute()
  })
})

const handleLogin = () => {
  console.log('点击登录按钮')
  // 这里可以添加登录逻辑
}
</script>

<style scoped>
.sidebar {
  width: 240px;
  height: 100vh;
  background-color: #F5F7FA;
  padding: 20px 0;
  position: relative;
  display: flex;
  flex-direction: column;
}

.logo-container {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
  padding: 0 30px;
}

.logo {
  max-width: 100%;
  height: auto;
}

.el-menu-vertical {
  border-right: none;
  background-color: #F5F7FA;
}

:deep(.el-menu-item) {
  font-size: 16px;
  padding: 0 30px;
  height: 56px;
  line-height: 56px;
  margin: 4px 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-sub-menu__title) {
  font-size: 16px;
  padding: 0 30px;
  height: 56px;
  line-height: 56px;
  margin: 4px 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-menu-item:hover),
:deep(.el-menu-item.is-active) {
  background-color: #FFFFFF !important;
  color: #303133;
}

:deep(.el-sub-menu__title:hover) {
  background-color: #FFFFFF !important;
}

:deep(.el-sub-menu .el-menu-item) {
  font-size: 20px;
  height: 48px;
  line-height: 48px;
  margin: 2px 12px;
  padding-left: 50px !important;
}

.menu-section-title {
  display: flex;
  align-items: center;
  font-size: 13px;
  color: #909399;
  padding: 16px 30px 8px 30px;
  margin: 0 12px;
  user-select: none;
  pointer-events: none;
  opacity: 0.8;
}

.menu-section-title .el-icon {
  margin-right: 8px;
  font-size: 14px;
}

.sub-menu-indent {
  padding-left: 5px;
}

.user-login-section {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 8px;
  border-top: 1px solid #E4E7ED;
  background-color: #FEFEFE;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.user-info:hover {
  background-color: #FFFFFF;
  /* box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08); */
}

.user-details {
  margin-left: 12px;
  display: flex;
  flex-direction: column;
}

.user-name {
  font-size: 14px;
  font-weight: 400;
  color: #303133;
}

.login-button-section {
  padding: 0 20px 20px 20px;
  margin-bottom: 30px;
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border: none;
  border-radius: 50px;
  background: linear-gradient(312deg, #67eb94, #3bb2f5);
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(103, 235, 148, 0.3);
}
.login-icon{
  margin-right: 5px;
}
.login-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(103, 235, 148, 0.4);
}

.login-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(103, 235, 148, 0.3);
}

</style>